#!/bin/bash
#configure
SUDO="/usr/bin/sudo"
NOHUP="/usr/bin/nohup"
KILL="/usr/bin/kill"
#MAIN_CMD="/usr/bin/php"                         #dev环境的php执行文件路径,用 which php 命令查看并修改该值
MAIN_CMD="/usr/local/php/bin/php"                  #pre和正式环境的php执行路径
#PATH_ARTISAN="/usr/share/nginx/ivy_dev/artisan" #dev环境的artisan文件路径
PATH_ARTISAN="/data/wwwroot/ivy_php/artisan" #pre和正式环境的artisan文件路径
#
DELAY_TIME=2
SLEEP_TIME=30                                  #若要使用supervisord管理进程,则要加上睡眠时间
#执行artisan的参数,有新的参数则按找顺序增加,如ARG2,ARG3

#2018-12-26修改:使用外部的文件来调用参数
ARG_FILEPATH="/data/wwwroot/ivy_php/php_artisan_queue_pre_arg.txt" #外部文件的路径

function start_process () {
    #index=1
    #val=$ARG1
    #num=$NUM1
    #user=$USER1
    #while [ "$val" != "" ]

    while read LINE || [[ -n ${LINE} ]]
    do
        CHECK=$( echo $LINE | grep "^#" ) 	#检查一行的开头是否为"#"符号，如果是，则认为该行为注释行，并忽略掉
        if [ "$CHECK" == "" ] ;then
            val=`echo $LINE | awk -F '[\"\"]' '{print$2}'`
            num=`echo $LINE | awk -F '[\"\"]' '{print$4}'`
            user=`echo $LINE | awk -F '[\"\"]' '{print$6}'`
            #echo "val is $val"
            #echo "num is $num"
            #echo "user is $user"
            if [ "$val" == "" ]||[ "$num" == "" ]||[ "$user" == "" ];then #检查参数是否完整
                echo "imcomplete arg!"
                continue #若参数不完整则跳过下面的代码,继续下一次循环
            else
                FULL_CMD=`echo $MAIN_CMD" "$PATH_ARTISAN" "$val`                #拼接启动命令
                echo "FULL_CMD is $FULL_CMD"
				if [ $num -eq 0 ];then      #如果在配置文件中配置了进程数量为0,则代表不考虑系统中实际存在多少个该进程,直接执行该命令一次
                	$SUDO -u $user $NOHUP $FULL_CMD &
                    sleep $DELAY_TIME
				else
                    process_num=`ps -ef | grep "$val" | grep $MAIN_CMD | grep -v $SUDO | awk '{print $2}' | wc -l` #查询进程数量
                    #echo "process num is: $process_num"
                    while [ $process_num -lt $num ]
                    do
                        $SUDO -u $user $NOHUP $FULL_CMD &
                        sleep $DELAY_TIME
                        process_num=`ps -ef | grep "$val" | grep $MAIN_CMD | grep -v $SUDO | awk '{print $2}' | wc -l`
                    done
				fi
            fi
        fi

    done < $ARG_FILEPATH

    if [ "$1" == "nosleep" ];then
        exit 0
    elif [ "$1" == "issleep" ];then
        sleep $SLEEP_TIME
        exit 0
    fi
}

function stop_process () {
    while read LINE || [[ -n ${LINE} ]]
    do
        CHECK=$( echo $LINE | grep "^#" )       #检查一行的开头是否为"#"符号，如果是，则认为该行为注释行，并忽略掉
        if [ "$CHECK" == "" ] ;then
            val=`echo $LINE | awk -F '[\"\"]' '{print$2}'`
            num=`echo $LINE | awk -F '[\"\"]' '{print$4}'`
            user=`echo $LINE | awk -F '[\"\"]' '{print$6}'`
            if [ "$val" == "" ]||[ "$num" == "" ]||[ "$user" == "" ];then #检查参数是否完整
                  continue
            else
                FULL_CMD=`echo $MAIN_CMD" "$PATH_ARTISAN" "$val`                #拼接启动命令
                worker_process=`ps -ef | grep "$val" | grep $MAIN_CMD | awk '{print $2}'`       #查找是否存在进程
                if [ "$worker_process" !=  "" ]; then #如果进程存在则杀死进程
                    $KILL -s 9 $worker_process
                fi
            fi
        fi
    done < $ARG_FILEPATH
}

function restart_queue () {
    #用于更新代码后重启队列
    stop_process
    start_process nosleep
}

case $1 in
    start)		start_process issleep;;
    start_nosleep)  start_process nosleep;;
    stop)		stop_process ;;
    restart)	restart_queue ;;
    *)	echo "usage: start/stop/restart"; exit 1 ;;
esac
exit 0


